package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.user.AddUserDTO;
import org.jsola.hr.dto.user.UpdateUserPasswordDTO;
import org.jsola.hr.dto.user.UpdateUserRoleDTO;
import org.jsola.hr.provider.IPermissionProviderService;
import org.jsola.hr.provider.IUserProviderService;
import org.jsola.hr.query.HrUserQuery;
import org.jsola.hr.service.IHrPermissionService;
import org.jsola.hr.vo.HrUserVO;
import org.jsola.permission.exception.PermissionException;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.jsola.user.query.UserQuery;
import org.jsola.user.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import static org.jsola.hr.constant.HrPermissionConstants.*;

/**
 * @author JanusMix
 */
@RestController("hrUserController")
@RequestMapping(HrConstants.API_PATH + "/v1/users")
public class UserController implements IUserControllerAPI {

    @Autowired
    private IUserProviderService userProviderService;

    @Autowired
    private IHrPermissionService hrPermissionService;

    @Autowired
    private IPermissionProviderService permissionProviderService;

    @Override
    @PostMapping(value = "/updateRole")
    public Result<String> updateUserRole(@RequestBody @Valid UpdateUserRoleDTO updateUserRoleDTO) {
        TokenUser tokenUser = UserContext.getUser();
//        PermissionException.isTrue(hrPermissionService.hasRight(tokenUser.getUserId(), tokenUser.getSiteId(), HR_ENTERPRISESETUP_ACCOUNTAUTHORITY), PERMISSION_DENIED_MESSAGE);
        userProviderService.updateUserRole(updateUserRoleDTO, tokenUser);
        return Result.success("执行成功");
    }

    @Override
    @PostMapping(value = "/updatePassword")
    public Result<String> updateUserPassword(UpdateUserPasswordDTO updateUserPasswordDTO) {
        TokenUser tokenUser = UserContext.getUser();
        PermissionException.isTrue(hrPermissionService.hasRight(tokenUser.getUserId(), tokenUser.getSiteId(), HR_ENTERPRISESETUP_ACCOUNTAUTHORITY), PERMISSION_DENIED_MESSAGE);
        userProviderService.updateUserPassword(updateUserPasswordDTO.getUserId(), updateUserPasswordDTO.getPassword(), tokenUser);
        return Result.success();
    }

    @Override
    @PostMapping(value = "/save")
    public Result<HrUserVO> saveUser(@RequestBody @Valid AddUserDTO addUserDTO) {
        TokenUser tokenUser = UserContext.getUser();
//        PermissionException.isTrue(hrPermissionService.hasRight(tokenUser.getUserId(), tokenUser.getSiteId(), HR_ENTERPRISESETUP_ACCOUNTAUTHORITY), PERMISSION_DENIED_MESSAGE);
        return Result.success(userProviderService.saveUser(addUserDTO, tokenUser));
    }

    @Override
    @GetMapping(value = "/list")
    public Result<List<HrUserVO>> queryUserList(@Valid HrUserQuery hrUserQuery) {
        TokenUser tokenUser = UserContext.getUser();
        PermissionException.isTrue(hrPermissionService.hasRight(tokenUser.getUserId(), tokenUser.getSiteId()
                , HR_ENTERPRISESETUP_ACCOUNTAUTHORITY), PERMISSION_DENIED_MESSAGE);
        // 返回权限小于自己的用户信息
        return Result.success(userProviderService.queryUserList(hrUserQuery, tokenUser));
    }

    @Override
    @GetMapping(value = "/dispatchList")
    public Result<List<HrUserVO>> queryDispatchUserList(@Valid HrUserQuery hrUserQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // 返回权限小于自己的用户信息
        return Result.success(userProviderService.queryUserList(hrUserQuery, tokenUser));
    }

    @Override
    @GetMapping(value = "/queryUserListLessToMe")
    public Result<List<HrUserVO>> queryUserListLessToMe(@Valid HrUserQuery hrUserQuery) {
        TokenUser tokenUser = UserContext.getUser();
//        PermissionException.isTrue(hrPermissionService.hasRight(tokenUser.getUserId(), tokenUser.getSiteId()
//                , HR_ENTERPRISESETUP_ACCOUNTAUTHORITY), PERMISSION_DENIED_MESSAGE);
        // 返回权限小于自己的用户信息
        return Result.success(userProviderService.queryUserListLessToMe(hrUserQuery, null, tokenUser));
    }

    @GetMapping(value = "/pageUserListLessToMe")
    public Result<Page<HrUserVO>> pageUserListLessToMe(@Valid HrUserQuery hrUserQuery) {
        TokenUser tokenUser = UserContext.getUser();
        PermissionException.isTrue(hrPermissionService.hasRight(tokenUser.getUserId(), tokenUser.getSiteId()
                , HR_ENTERPRISESETUP_ACCOUNTAUTHORITY), PERMISSION_DENIED_MESSAGE);
        // 返回权限小于自己的用户信息
        return Result.success(userProviderService.pageUserListLessToMe(hrUserQuery, tokenUser));
    }

    @GetMapping(value = "/queryUserListLessToMeAdd")
    public Result<List<HrUserVO>> queryUserListLessToMeAdd(@Valid HrUserQuery hrUserQuery) {
        TokenUser tokenUser = UserContext.getUser();
        PermissionException.isTrue(hrPermissionService.hasRight(tokenUser.getUserId(), tokenUser.getSiteId()
                , HR_ENTERPRISESETUP_ACCOUNTAUTHORITY), PERMISSION_DENIED_MESSAGE);
        // 返回权限小于自己的用户信息
        List<HrUserVO> result = userProviderService.queryUserListLessToMe(hrUserQuery, null, tokenUser);
        userProviderService.addSuperAdmin(result, tokenUser);
        return Result.success(result);
    }

    @Override
    @GetMapping(value = "/listUserHasRoleInCompany/{companyId}")
    public Result<List<HrUserVO>> listUserHasRoleInCompany(@PathVariable String companyId,
                                                           @Valid UserQuery userQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                userProviderService.listUserHasRoleInCompany(userQuery, companyId, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/pageUserHasRoleInCompany/{companyId}")
    public Result<Page<HrUserVO>> pageUserHasRoleInCompany(@PathVariable String companyId,
                                                           @Valid UserQuery userQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                userProviderService.pageUserHasRoleInCompany(userQuery, companyId, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/listUserHasRoleInSite")
    public Result<List<HrUserVO>> listUserHasRoleInSite(@Valid UserQuery userQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                userProviderService.listUserHasRoleInSite(userQuery, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/pageUserHasRoleInSite")
    public Result<Page<HrUserVO>> pageUserHasRoleInSite(@Valid UserQuery userQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                userProviderService.pageUserHasRoleInSite(userQuery, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/listEmpAndRoleUserInCompany/{companyId}")
    public Result<List<HrUserVO>> listEmpAndRoleUserInCompany(@PathVariable String companyId,
                                                              @Valid UserQuery userQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                userProviderService.listEmpAndRoleUserInCompany(userQuery, companyId, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/pageEmpAndRoleUserInCompany/{companyId}")
    public Result<Page<HrUserVO>> pageEmpAndRoleUserInCompany(@PathVariable String companyId,
                                                              @Valid UserQuery userQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                userProviderService.pageEmpAndRoleUserInCompany(userQuery, companyId, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/simplePage")
    public Result<Page<HrUserVO>> querySimpleUserPage(UserQuery userQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(userProviderService.querySimpleUserPage(userQuery, tokenUser.getSiteId()));
    }

    @Override
    @PostMapping(value = "/dimission")
    public Result<String> dimissionUsers(@RequestBody String[] userIds) {
        TokenUser tokenUser = UserContext.getUser();
        PermissionException.isTrue(hrPermissionService.hasRight(tokenUser.getUserId(), tokenUser.getSiteId(), HR_ENTERPRISESETUP_ACCOUNTAUTHORITY), PERMISSION_DENIED_MESSAGE);
        ParamException.notNull(userIds, "请输入要离职的用户id");
        userProviderService.dimissionUser(userIds, tokenUser);
        return Result.success("执行成功");
    }

    @Override
    @PostMapping(value = "/delete")
    public Result<String> deleteUsers(@RequestBody String[] userIds) {
        TokenUser tokenUser = UserContext.getUser();
//        PermissionException.isTrue(hrPermissionService.hasRight(tokenUser.getUserId(), tokenUser.getSiteId(), HR_ENTERPRISESETUP_ACCOUNTAUTHORITY), PERMISSION_DENIED_MESSAGE);
        ParamException.notNull(userIds, "请输入要删除的用户id");
        userProviderService.deleteUser(userIds, tokenUser);
        return Result.success("执行成功");
    }

    @Override
    @PostMapping(value = "/updateAllUserPinyin")
    public Result<String> updateAllUserPinyin() {
        userProviderService.updateAllUserPinyin();
        return Result.success();
    }

    @Override
    @GetMapping("/userIsSuperme")
    public Result<Integer> userIsSuperme() {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                permissionProviderService.userIsSuperme(tokenUser)
        );
    }

    @Override
    @GetMapping("/getCurrentUserInfo")
    public Result<HrUserVO> getCurrentUserInfo() {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                permissionProviderService.getCurrentUserInfo(tokenUser)
        );
    }

    @Override
    @GetMapping("/smallProgramGetCurrentUserInfo")
    public Result<HrUserVO> smallProgramGetCurrentUserInfo() {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                permissionProviderService.smallProgramGetCurrentUserInfo(tokenUser)
        );
    }

    @Override
    @GetMapping("/selectHadRoleUserIdBySiteId")
    public Result<List<UserVO>> selectHadRoleUserIdBySiteId() {
        TokenUser tokenUser = UserContext.getUser();

        List<String> userIds = permissionProviderService.selectHadRoleUserIdBySiteId(tokenUser);
        if (CollectionUtils.isEmpty(userIds)) {
            return Result.success();
        }

        return Result.success(
                userProviderService.selectUserByIds(userIds).parallelStream().map(userDO
                        -> userDO.to(UserVO.class)).collect(Collectors.toList())
        );
    }

    @GetMapping(value = "/queryUserJiaFang")
    public Result<List<HrUserVO>> queryUserJiaFang(@Valid HrUserQuery hrUserQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // 返回权限小于自己的用户信息
        return Result.success(userProviderService.queryUserJiaFang(hrUserQuery, null, tokenUser));
    }

}
